import { Component, OnInit } from '@angular/core';
import { Router, NavigationEnd, ActivatedRoute } from '@angular/router';
import { NzModalRef, NzModalService } from 'ng-zorro-antd';
import { UtilService } from '../my-service/util.service';

import 'rxjs/add/operator/filter';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/mergeMap';


@Component({
  selector: 'nav-root',
  templateUrl: './nav.component.html',
  styleUrls: ['./nav.component.css']
})
export class NavComponent implements OnInit{
  activeMenu: string = 'home';
  confirmModal: NzModalRef;
  stuGuid:string;
  routerIndex:number = 0; //当前激活的菜单栏
  searchKey:any =null; //搜索关键字
  constructor(
    private router: Router,
    private activatedRoute: ActivatedRoute,
    private modal: NzModalService,
    private utilService: UtilService,
  ) {
  this.matchMenu();
  }
 getUserGuid() {
    this.utilService.getUserInfo().then(res=>{
      if (res.status ==1) {
        this.stuGuid  = res.userGuid;
      }
    })
 }
  ngOnInit() {
    this.getUserGuid();
    this.router.events
    .filter(event => event instanceof NavigationEnd)
    .map(() => this.activatedRoute)
    .map(route => {
      while (route.firstChild) route = route.firstChild;
      return route;
    })
    .filter(route => route.outlet === 'primary')
    .mergeMap(route => route.data)
  .subscribe(event => {
      this.activeMenu = event.menu;
    });
  }
  goHome() {
    this.router.navigate(['/']);
  }
  routerMap (index) {
    this.routerIndex = index;
    if(this.routerIndex == 0){
      this.router.navigate(['home/start']);
    }else {
      this.router.navigate(['home/joinRoll',{search:this.searchKey}]);
    }
  }
  matchMenu() {
     if(/home\/joinRoll/g.test(location.href)){
        this.routerIndex =1;
     }else {
       this.routerIndex = 0;
     }
  }
  search(){
    this.router.navigate(['home/joinRoll',{search:this.searchKey}]);
    this.routerIndex =1;
  }
}
